<?
	session_start();
	include('conn.php');
	$obj = json_decode($_REQUEST["dataset"]);
	$opt = $_REQUEST["q"];
	$user=$_SESSION['username'];
	
	switch($opt){
		case 'c':
			$qrystb = "select s.stbtypeid, st.id as stid, ct.id as ctid from STBtype s, stbTable st, cardTable ct
					where STBType='$obj->typename' and 
					st.id in (select id from stbTable where stbno = '$obj->num') and
					ct.id in (select id from cardTable where cardno = '$obj->card');";
			$resstb = mysql_query($qrystb);	
			$rowstb = mysql_fetch_array($resstb);
			$typeid = $rowstb['stbtypeid'];
			$stbid = $rowstb['stid'];
			$cardid = $rowstb['ctid'];
			
			$qry = "insert into STBDetails(stbid,cardid,stbtypeid,Active,Notes,assigned,enteredby,entereddate) 
			values('$stbid', '$cardid', '$typeid', '1','$obj->note','n','$user','".date('Y-m-d')."');";	
			echo "<br> $qry <br>";
			$res = mysql_query($qry);	

			$qry = "update stbTable set flag='occ' where id='$stbid'";				
			$res = mysql_query($qry);
			$qry1 = "update cardTable set flag='occ' where id='$cardid'";				
			$res1 = mysql_query($qry1);
			echo "<br> $qry <br> $qry1";
			break;
		
		case 'u':
			$qrystb = "select s.stbtypeid, st.id as stid, ct.id as ctid from STBtype s, stbTable st, cardTable ct
					where STBType='$obj->typename' and 
					st.id = (select id from stbTable where stbno = '$obj->num') and
					ct.id = (select id from cardTable where cardno = '$obj->card');";
			$resstb = mysql_query($qrystb);	
			$rowstb = mysql_fetch_array($resstb);
			$typeid = $rowstb['stbtypeid'];
			$stbid = $rowstb['stid'];
			$cardid = $rowstb['ctid'];						
			$id = $obj-> id;
			
			$qry = "select stbid,cardid from STBDetails where pairid='$id';";
			$row = mysql_fetch_array(mysql_query($qry));
			$oldstb = $row['stbid'];
			$oldc = $row['cardid'];
			echo "...$oldstb $stbid".($oldstb == $stbid)."--".($oldc != $cardid);
			
			if($oldstb != $stbid){
				$qry = "update stbTable s set s.flag='free' where s.id='$oldstb';";				
				$res = mysql_query($qry);
				echo "<br>--$qry -> $res";
			}
			if($oldc != $cardid){
				$qry = "update cardTable c set c.flag='free' where c.id='$oldc'";				
				$res = mysql_query($qry);
				echo "<br>--$qry -> $res";
			}
			
			$qry = "update STBDetails
			set stbid= '$stbid',cardid= '$cardid', Notes= '$obj->note', stbtypeid= '$typeid',
			modifiedby = '$user', modifieddate = '".date('Y-m-d')."' where pairid='$id';";	
			echo "<br> $qry <br>";
			$res = mysql_query($qry);

			$qry = "update stbTable set flag='occ' where id='$stbid'";				
			$res = mysql_query($qry);
			$qry1 = "update cardTable set flag='occ' where id='$cardid'";				
			$res1 = mysql_query($qry1);
			echo "<br> $qry <br> $qry1";
			break;
			
		case 'd':
			$id = $_REQUEST['id'];
			$qry = "update STBDetails set Active='9' where pairid='$id';";		
			echo "<br> $qry <br>";
			$res = mysql_query($qry);

			$qry ="update stbTable set flag='free' where id=
				(select stbid from STBDetails where pairid='$id')";	
			$res = mysql_query($qry);
			$qry1="update cardTable set flag='free' where id=
				(select cardid from STBDetails where pairid='$id')";				
			$res1 = mysql_query($qry1);
			echo "<br> $qry <br> $qry1";
			break;
		
		default:
			echo "No option selected";
	}
?>